<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
    <script src="/webjars/bootstrap/3.3.7/js/bootstrap.js" type="text/javascript" rel="script"></script>
    <script src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script src="/webjars/axios/0.19.0/dist/axios.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/webjars/bootstrap/3.3.7/css/bootstrap.css"/>
    <script src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script src="/config.js"></script>
</head>
<body>
<div id="vuebox">
    欢迎您:<span v-text="username"></span>
    <button class="btn btn-default" type="button" @click="logout()">退出登录</button>
    <a class="btn btn-primary" href="/apply.html">申请</a>
    <table class="table table-bordered">
        <caption>当前登录用户剩余任务</caption>
        <thead>
        <tr>
            <th>任务编号</th>
            <th>所处节点名称</th>
            <th>实例编号</th>
            <th>定义编号</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in tasks">
            <td v-text="item.id"></td>
            <td v-text="item.name"></td>
            <td v-text="item.processInstanceId"></td>
            <td v-text="item.processDefinitionId"></td>
            <td v-text="item.createdDate"></td>
            <td>
                <button class="btn btn-warning" type="button" @click="checkDetail(item.id)">查看详情</button>
                <a class="btn btn-success" @click="exe(item.id,'yes')" v-if="role=='admin'">同意</a>
                <a class="btn btn-danger" @click="exe(item.id,'no')" v-if="role=='admin'">驳回</a>
                <a class="btn btn-success" @click="hrexe(item.id,'yes')" v-if="role=='hr'">同意</a>
                <a class="btn btn-danger" @click="hrexe(item.id,'no')" v-if="role=='hr'">驳回</a>
                <a class="btn btn-danger" @click="edit(item.id)" v-if="role=='user'">修改</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>


</body>
</html>
<script>
    var vm=new Vue({
        el:"#vuebox",
        data:{
            role:"",
            username:"",
            tasks:{},
            detailParam:{
                taskId:""
            },
            exeParam:{
                taskId:"",
                isAgree:""
            }
        },
        methods:{
            edit:function(taskId){
                this.detailParam.taskId=taskId;
                axios.post("http://localhost:9999/queryTaskData",this.detailParam,{
                    headers:                  {'token':sessionStorage.getItem("token")}
                }).then(res=>{
                    if (res.data.code=="200"){
                        sessionStorage.setItem("state","userApply");
                        sessionStorage.setItem("day",res.data.data.day);
                        sessionStorage.setItem("cause",res.data.data.cause);
                        sessionStorage.setItem("taskId",taskId);
                        window.location.href="/apply.html";
                    }else{
                        console.log(res.data.message);
                    }
                })
            },
            exe:function(taskId,isAgree){
                this.exeParam.taskId=taskId;
                this.exeParam.isAgree=isAgree;
                axios.post("http://localhost:9999/adminTask",this.exeParam,{
                    headers:  {'token':sessionStorage.getItem("token")}
                }).then(res=>{
                    this.queryTask();
                    alert(res.data.data)
                })
            },
            hrexe:function(taskId,isAgree){
                this.exeParam.taskId=taskId;
                this.exeParam.isAgree=isAgree;
                axios.post("http://localhost:9999/hrTask",this.exeParam,{
                    headers:  {'token':sessionStorage.getItem("token")}
                }).then(res=>{
                    this.queryTask();
                    alert(res.data.data)
                })
            },
            logout:function(){
                localStorage.clear();
                window.location.href="/login.html";
            },
            checkDetail:function (id) {
                this.detailParam.taskId=id;
                axios.post("http://localhost:9999/queryTaskData",this.detailParam,{
                    headers:                  {'token':sessionStorage.getItem("token")}
                }).then(res=>{
                   if (res.data.code=="200"){
                       alert("用户:"+res.data.data.user+"\n申请天数:"+res.data.data.day+"\n申请原因:"+res.data.data.cause);
                   }else{
                       alert(res.data.message);
                   }
                })
            }
            ,
            queryTask:function () {
                axios({
                    method:"post",
                    url:"http://localhost:9999/queryTask",
                    param:{},
                    headers:{
                        'token':sessionStorage.getItem("token")
                    }
                }).then(res=>{
                    if (res.data.code=="200"){
                        this.tasks=res.data.data.content;
                    }else{
                        alert(res.data.message);
                    }
                })
            }
        },
        mounted(){
            this.queryTask();
            this.role=sessionStorage.getItem("role");
            this.username=sessionStorage.getItem("username");

        }
    })

</script>